#!/bin/bash

set -e

otaFile="$1"

if [ -z "$otaFile" ] || [ ! -f "$otaFile" ]; then
    echo "Usage: $0 <path-to-OTA-file>"
    exit 1
fi

if [ ! -d "common" ]; then
    echo "Please run this script from the root of the repository!"
    exit 1
fi

rm -rf /tmp/ota
sudo umount /mnt/system 2>/dev/null || true
sudo umount /mnt/product 2>/dev/null || true
sudo umount /mnt/system_ext 2>/dev/null || true

echo "Extracting payload file from $otaFile..."
unzip -qq -o "$otaFile" -d /tmp/ota

echo "Extracting partitions from payload file..."
payload_dumper --partitions product,system,system_ext --out /tmp/ota/payload /tmp/ota/payload.bin &>/dev/null

echo "Copying proprietary files from /system..."
sudo mkdir -p /mnt/system
sudo mount -t ext4 -o ro /tmp/ota/payload/system.img /mnt/system
cp -r /mnt/system/system/app/GoogleExtShared ./common/proprietary/app
cp -r /mnt/system/system/app/GooglePrintRecommendationService ./common/proprietary/app
cp -r /mnt/system/system/priv-app/GooglePackageInstaller ./common/proprietary/priv-app
sudo umount /mnt/system
sudo rm -rf /mnt/system

echo "Copying proprietary files from /product..."
sudo mkdir -p /mnt/product
sudo mount -t ext4 -o ro /tmp/ota/payload/product.img /mnt/product
cp -r /mnt/product/app/arcore ./common/proprietary/product/app
cp -r /mnt/product/app/CalculatorGooglePrebuilt ./common/proprietary/product/app
cp -r /mnt/product/app/CalendarGooglePrebuilt ./common/proprietary/product/app
cp -r /mnt/product/app/Chrome ./common/proprietary/product/app
cp -r /mnt/product/app/Chrome-Stub ./common/proprietary/product/app
cp -r /mnt/product/app/GoogleContacts ./common/proprietary/product/app
cp -r /mnt/product/app/GoogleTTS ./arm64/proprietary/product/app
cp -r /mnt/product/app/LatinIMEGooglePrebuilt ./arm64/proprietary/product/app
cp -r /mnt/product/app/LocationHistoryPrebuilt ./common/proprietary/product/app
cp -r /mnt/product/app/Maps ./arm64/proprietary/product/app
cp -r /mnt/product/app/Photos ./arm64/proprietary/product/app
cp -r /mnt/product/app/PrebuiltDeskClockGoogle ./common/proprietary/product/app
cp -r /mnt/product/app/PrebuiltGmail ./arm64/proprietary/product/app
cp -r /mnt/product/app/talkback ./arm64/proprietary/product/app
cp -r /mnt/product/app/TrichromeLibrary ./common/proprietary/product/app
cp -r /mnt/product/app/TrichromeLibrary-Stub ./common/proprietary/product/app
cp -r /mnt/product/app/WebViewGoogle ./common/proprietary/product/app
cp -r /mnt/product/app/WebViewGoogle-Stub ./common/proprietary/product/app
cp -r /mnt/product/priv-app/AndroidAutoStubPrebuilt ./common/proprietary/product/priv-app
cp -r /mnt/product/priv-app/ConfigUpdater ./common/proprietary/product/priv-app
cp -r /mnt/product/priv-app/GoogleDialer ./arm64/proprietary/product/priv-app
cp -r /mnt/product/priv-app/GoogleOneTimeInitializer ./common/proprietary/product/priv-app
cp -r /mnt/product/priv-app/GoogleRestorePrebuilt-v445524 ./common/proprietary/product/priv-app
cp -r /mnt/product/priv-app/PartnerSetupPrebuilt ./common/proprietary/product/priv-app
cp -r /mnt/product/priv-app/Phonesky ./arm64/proprietary/product/priv-app
cp -r /mnt/product/priv-app/PrebuiltBugle ./arm64/proprietary/product/priv-app
cp -r /mnt/product/priv-app/PrebuiltGmsCore ./common/proprietary/product/priv-app
cp -r /mnt/product/priv-app/SetupWizardPrebuilt ./arm64/proprietary/product/priv-app
cp -r /mnt/product/priv-app/Velvet ./arm64/proprietary/product/priv-app
cp -r /mnt/product/priv-app/WellbeingPrebuilt ./common/proprietary/product/priv-app
cp ./common/proprietary/product/priv-app/PrebuiltGmsCore/m/independent/AndroidPlatformServices.apk ./common/proprietary/product/priv-app/AndroidPlatformServices
sudo umount /mnt/product
sudo rm -rf /mnt/product

echo "Copying proprietary files from /system_ext..."
sudo mkdir -p /mnt/system_ext
sudo mount -t ext4 -o ro /tmp/ota/payload/system_ext.img /mnt/system_ext
cp -r /mnt/system_ext/priv-app/GoogleFeedback ./common/proprietary/system_ext/priv-app
cp -r /mnt/system_ext/priv-app/GoogleServicesFramework ./common/proprietary/system_ext/priv-app
sudo umount /mnt/system_ext
sudo rm -rf /mnt/system_ext

echo "Cleaning up..."
find ./{arm,arm64,common}/proprietary -type f -name "*.prof" -delete 2>/dev/null || true
find ./{arm,arm64,common}/proprietary -type d -name "oat" -exec rm -rf {} \; 2>/dev/null || true
rm -rf ./common/proprietary/product/priv-app/PrebuiltGmsCore/app_chimera
rm -rf ./common/proprietary/product/priv-app/PrebuiltGmsCore/m
rm -rf /tmp/ota

echo "Finished!"
